package sc.common.test;


public class ReflectionUtil {

    public static boolean implInterfaceShallow(Class<?> impl, Class<?> itf) {
        Class<?>[] itfs = impl.getInterfaces();
        for (Class<?> it : itfs) {
            if (it == itf) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        JobStopWatchBack w = new JobStopWatchBack("ReflectionUtil Test");
        w.begin("Function");
        test(String.class, Comparable.class, false);
        test(Integer.class, Comparable.class, true);
        test(Integer.class, String.class, false);
        w.end();
        System.out.println("All cases passed ~");

        w.begin("Performance implInterfaceShallow 1000000");
        for (int i = 0; i != 1000000; i++) {
            implInterfaceShallow(String.class, Comparable.class);
        }
        w.end();
        System.out.println(w);
    }

    static void test(Class<?> impl, Class<?> itf, boolean expected) {
        boolean actual = implInterfaceShallow(impl, itf);
        System.out.println(String.format("[%-40s] implements [%-50s] -> %s", impl, itf, actual));
        if (expected != actual) {
            throw new IllegalArgumentException();
        }
    }
}
